package com.example.erp.config.error;

import cn.dev33.satoken.exception.NotLoginException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class ErrorControllerAdvice {


    @ExceptionHandler(value = Exception.class)
    @ResponseStatus(HttpStatus.OK)
    public Map handler(Exception ex) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 500);
        map.put("msg",ex.getMessage());
        ex.printStackTrace();
        return map;
    }

    @ExceptionHandler(value = CustomException.class)
    @ResponseStatus(HttpStatus.OK)
    public Map handlerCustomer(Exception ex) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 500);
        map.put("msg",ex.getMessage());
        ex.printStackTrace();
        return map;
    }

    @ExceptionHandler(DuplicateKeyException.class)
    @ResponseStatus(HttpStatus.OK)
    public Map handlerSQLException(SQLException e)
    {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 500);
        map.put("msg","数据重复");
        e.printStackTrace();
        return map;


    }


    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseStatus(HttpStatus.OK)
    public Map handlerHttpMessageNotReadableException(HttpMessageNotReadableException e)
    {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 500);
        map.put("msg","数据格式错误");
        e.printStackTrace();
        return map;

    }

    @ExceptionHandler(NotLoginException.class)
    @ResponseStatus(HttpStatus.OK)
    public Map handlerNotLoginException(NotLoginException e)
    {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 401);
        map.put("msg","token无效");
        e.printStackTrace();
        return map;

    }
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.OK)
    public Map handlerMethodArgumentNotValidException(MethodArgumentNotValidException e)
    {
        BindingResult bindingResult=e.getBindingResult();
        StringBuilder sb=new StringBuilder("校验失败:");
        for(FieldError fieldError: bindingResult.getFieldErrors()){
            sb.append(fieldError.getDefaultMessage()).append(", ");
        }
        String msg=sb.toString();
        Map<String, Object> map = new HashMap<>();
        map.put("code", 500);
        map.put("msg",msg);
        e.printStackTrace();
        return map;

    }



}
